package com.wang.vedio.adapter.index;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.youth.banner.adapter.BannerAdapter;
import com.wang.vedio.R;
import com.wang.vedio.bean.BannerBean;
import com.wang.vedio.net.rxjava.HttpMethods;

import java.util.List;

public class BannerImageAdapter extends BannerAdapter<BannerBean, BannerImageAdapter.BannerImageViewHolder> {

    public BannerImageAdapter(List<BannerBean> datas) {
        super(datas);
    }

    @Override
    public BannerImageViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        ImageView imageView = new ImageView(parent.getContext());
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(params);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new BannerImageViewHolder(imageView);
    }

    @Override
    public void onBindView(BannerImageViewHolder holder, BannerBean data, int position, int size) {
        //通过图片加载器实现圆角，你也可以自己使用圆角的imageview，实现圆角的方法很多，自己尝试哈
//        Glide.with(holder.itemView)
//                .load(HttpMethods.BASE_URL+data.getAdImg())
//                .thumbnail(Glide.with(holder.itemView).load(R.drawable.ic_bg_wel1))
//                .apply(RequestOptions.bitmapTransform(new MultiTransformation<>(new CenterCrop(),new RoundedCorners(30))))
//                .into(holder.imageView);
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.logoss)//图片加载出来前，显示的图片
                .fallback(R.mipmap.logoss) //url为空的时候,显示的图片
                .error(R.mipmap.logoss);//图片加载失败后，显示的图片
        Glide.with(holder.imageView).load(HttpMethods.BASE_URL+data.getAdImg()).apply(options).into(holder.imageView);
    }

    class BannerImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        public BannerImageViewHolder(@NonNull View itemView) {
            super(itemView);
            this.imageView = (ImageView) itemView;
        }
    }
}
